<?php
/**
 * File: messageboxclass.php
 * 
 * This file contains the class that creates new messages.
 */
/**
 * 
 */
require_once 'lib/config.php';
require_once 'iHackyFunctions.php';
require_once 'lib/mockajaxfbconfig.php';

/**
 * MessageBox Class
 * Class that retrieves and displays messages sent to particular user.
 * @PARAM $id, user's Facebook ID, $messages, messages from the database.
 * 
 */
class MessageBox
{	
	private $id;
	private $messages;
	private $numberOfNewMessages;

	// Constructor for MessageBox class
	// Opens connection with database
	// Retrieves table of all messages corresponding to id.
	public function __construct($id)
	{
		$this->id = $id;
		$conn = getDbConn();

		$query = "SELECT * FROM MESSAGES m WHERE m.recipient = '$this->id'" .
			 "ORDER BY m.time_sent DESC";
		$this->messages = mysql_query($query, $conn);

	}
	// Destructor for MessageBox class
	// Closes connection with database, the one opened by the Constructor.
	public function __destruct()
	{
		// closes database connection.
		closeDb($conn);
	}

	// Function that lists all the messages of a particular user, by Facebook id.
	// Must use along with formatMessages() because of HTML table format.
	// PARAM: Nothing
	// RETURN: $count, number of messages listed.
	public function listMessages($show)
	{
		if($show)
		{
			echo "<table border=0>";
			echo "<tr>";
			echo "<td style=\"width: 100px; background-color: #EEEEEE; \">Status:</td>";
			echo "<td style=\"width: 100px; background-color: #EEEEEE; \">Message ID:</td>";
			echo "<td style=\"width: 100px; background-color: #EEEEEE; \">Timestamp:</td>";
			echo "<td style=\"width: 100px; background-color: #EEEEEE; \">Sender:</td>";
			echo "<td style=\"width: 100px; background-color: #EEEEEE; \">Message Subject:</td>";
			echo "</tr>";
			echo "</table>";

			$count = 0;
			$numberOfNewMessages = 0;
			while ($row = mysql_fetch_array($this->messages))
			{
				$count++;			
				$this->formatMessage($row[0], $row[1], $row[2], $row[3], $row[4],$row[5], $row[6]);
				if($row[6] == 0) {
					$numberOfNewMessages++;
				}
			}
			$this->numberOfNewMessages = $numberOfNewMessages;
			// The table is closed here to maintain format of the rows within.
			echo "</table>";
			return $count;
		}
		else {
			
			$count = 0;
			$numberOfNewMessages = 0;
			while ($row = mysql_fetch_array($this->messages))
			{
				$count++;
				if($row[6] == 0) {
					$numberOfNewMessages++;
				}
			}
			$this->numberOfNewMessages = $numberOfNewMessages;
			return $count;
		}
	}

	// Format each message into a table like structure.
	// PARAM: $message_id, $sender, $recipient, $messageSubject, $messageBody, $errorData, $timestamp (obvious)
	// RETURN: Nothing
	public function formatMessage($message_id, $sender, $recipient, $messageSubject, $messageBody, $timestamp, $status)
	{
		$formattedMessageBody = htmlentities($messageBody);
		//$formattedErrorData = htmlentities($errorData);
		$formattedMessageSubject = htmlentities($messageSubject);
		$messageHandler = "store_message.php";
		$formattedStatus = "0";

		if ($status == 0) {
			$formattedStatus = "New";
		}
		if ($status == 1) {
			$formattedStatus = "_";
		}
		
		echo "<table border=0 style=\"background-color: #FFFFFF\">";
		echo "<tr>";
		echo "<td style=\"width: 100px;\">";
		echo "<a onclick=\"showdiv('$message_id');\">$formattedStatus</a>";
		echo "</td>";
		echo "<td style=\"width: 100px;\">";
		echo "<a onclick=\"showdiv('$message_id');\">ID: $message_id</a>";
		echo "</td>";
		echo "<td style=\"width: 100px;\">";
		echo "<a onclick=\"showdiv('$message_id');\">$timestamp</a>";
		echo "</td>";
		echo "<td style=\"width: 100px;\">";
		echo "<a onclick=\"showdiv('$message_id');\"><fb:name useyou=\"false\" uid=\"$sender\"/></a>";
		echo "</td>";
		echo "<td style=\"width: 100px;\">";		
		echo "<a onclick=\"showdiv('$message_id');\">$formattedMessageSubject</a>";
		echo "</td>";
		echo "<td style=\"width: 100px;\">";
		echo "<form name=\"read\" method=\"post\" action=\"handle_message.php\">";
		echo "<input name=\"read\" type=\"submit\" style=\"width: 100px\" value=\"Mark As Read\">";
		echo "<input name=\"message_id\" type=\"hidden\" value=\"$message_id\">";
		echo "</form>";
		echo "</td>";
		echo "</tr>";
		echo "</table>";

		// This div will appear when the showdiv() Javascript function below invokes.
		// Within the div is a table of the message content.
		echo "<div id=\"$message_id\" style=\"display: none\">";
		echo "<table border=0 style=\"background-color: #EEEEEE \">";
		echo "<tr>";

		echo "<td style=\"width: 100px;\" valign=\"top\">";
		echo "From: ";
		echo "</td>";
		echo "<td style=\"width: 408px;\" valign=\"top\">";
		echo "<fb:name useyou=\"false\" uid=\"$sender\" />";
		echo "</td>";
		echo "</tr>";
		echo "</table>";

		// breaking table because createMessageForm() function is a table itself.
		echo "<table border=0>";
		echo "<tr>";
		echo "<td>";
		$message_id_reply = $message_id . "reply";
                echo "<a onclick=\"showdiv('$message_id_reply');\"><input name=\"reply\" type=\"submit\" style=\"width: 100px\" value=\"Reply\"></a>
                <div id=\"$message_id_reply\" style=\"display: none\">"; 

	        $replyMessage = new Message($recipient, $sender, $formattedMessageSubject, $formattedMessageBody, "Time", "0", $messageHandler);
	        $replyMessage->createMessageForm();

	        echo "</div><br/>";
		echo "</td>";
		echo "</tr>";
		echo "</table>";

	
		echo "<table>";

		echo "<tr>";
		echo "<td>";

		echo "<form name\"delete\" method=\"post\" action=\"handle_message.php\">";
		echo "<input type=\"submit\" name=\"delete\" style=\"width: 100px\" value=\"Delete\">";
		echo "<input name=\"message_id\" type=\"hidden\" value=\"$message_id\" >";
		echo "</form>";

		echo "</td>";
		echo "</tr>";

		echo "<tr>";
		echo "<td style=\"width: 100px;\" valign=\"top\">";
		echo "Message Body: ";
		echo "</td>";
		echo "<td style=\"width: 400px;\">";
		echo "$formattedMessageBody";
		echo "</td>";
		echo "</tr>";
		echo "</table>";
		// echo "</form>";
		echo "</div>";

		echo "<script type=\"text/javascript\">
		
		// Function to set a HTML div to show and hide;
		// PARAM: id, the div id to show or hide.
		// RETURN: False, always
		function showdiv(id) {
			var elm = document.getElementById(id);
			if(elm.getStyle('display') == 'block') {
				Animation(elm).to('height', '0px').to('opacity', 0).hide().ease(Animation.ease.end).go();			
				return false;
			}
			else {
				Animation(elm).to('height', 'auto').to('opacity', 1).show().ease(Animation.ease.end).go();
				return false;
			}
		}
		</script>";

	}

	// Setter for id
	// PARAM: $id, Facebook ID of Message Box owner.
	// RETURN: Nothing
	public function setId($id)
	{
		$this->id = $id;
	}

	// Getter for id
	// PARAM: Nothing
	// RETURN: $id, the Message Box's' owner's' Facebook ID.
	public function getId()
	{
		return $this->id;
	}

	public function getNumberOfNewMessages()
	{
		return $this->numberOfNewMessages;
	}
	
} // end class


// Creating a Message box for $user (self) and lists the messages.

/*
$messageBox = new MessageBox($user);
$messageBoxSize = $messageBox->listMessages(true);
$messageUnread = $messageBox->getNumberOfNewMessages();
echo "<br><br>There are $messageBoxSize message(s). $messageUnread unread. <br>";
*/

?>